package xyz.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Locale;
import java.util.Map;

import xyz.dao.NoteDatabase;
import xyz.entity.Note;
import xyz.test.R;
import xyz.test.ShowNoteActivity;
import xyz.viewmodel.NoteViewModel;

public class NoteListFragment extends Fragment {

    private View mView;
    private ListView mlistView;
    private List<Note> mNoteList;
    private Button mOkButton;

    public static Fragment getNoteListFragment() {
        Fragment fragment = new NoteListFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.note_fragment_list, container, false);
        mlistView = mView.findViewById(R.id.list_notes);

        return mView;
        //return super.onCreateView(inflater,container,savedInstanceState);
    }

    private List<Map<String, String>> maps = new ArrayList<>();

    private SimpleAdapter simpleAdapter;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        LiveData<List<Note>> listLiveData = new ViewModelProvider(getActivity()).get(NoteViewModel.class)
                .getNoteLiveData();
        listLiveData.observe(getActivity(), new Observer<List<Note>>() {
            @Override
            public void onChanged(@Nullable List<Note> notes) {
                mNoteList = notes;
                maps.clear();
                for (Note note : notes) {
                    Map<String, String> map = new HashMap<>();
                    map.put("title", note.getTitle());
                    map.put("content", note.getContent());
                    DateFormat dateFormat = new SimpleDateFormat("YYYY/MM/dd", Locale.CHINA);
                    map.put("date", dateFormat.format(note.getCreated()));
                    maps.add(map);
                }
                simpleAdapter.notifyDataSetChanged();
            }
        });
        maps = new ArrayList<>();
        simpleAdapter = new SimpleAdapter(getActivity(), maps, R.layout.listview_notes_item,
                new String[]{"title", "date", "content"}, new int[]{R.id.tv_item_title, R.id.tv_item_date, R.id.tv_item_content});
        mlistView.setAdapter(simpleAdapter);
        mlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), ShowNoteActivity.class);
                intent.putExtra("id", mNoteList.get(position).getId());
                startActivity(intent);
            }
        });

        mlistView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int pos, long l) {
                new AlertDialog.Builder(getContext())
                        .setMessage("是否删除")
                        .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {


                                new ViewModelProvider(getActivity()).get(NoteViewModel.class).delete(mNoteList.get(pos));
                            }
                        })
                        .setNegativeButton(android.R.string.cancel, null).show();

                return true;
            }
        });

    }


    private List<Map<String, String>> getData() {
        List<Map<String, String>> list = new ArrayList<>();
        for (Note note : mNoteList) {
            Map<String, String> map = new HashMap<>();
            map.put("title", note.getTitle());
            map.put("content", note.getContent());

            list.add(map);
        }
        return list;
    }
}
